package com.example.fw;

import java.util.HashSet;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class GroupHelper extends HelperWithWebDriverBase {
	
//	private Groups groupCache;
	private Groups groupModel;
    private int lastGroupId;

	public GroupHelper (ApplicationManager manager) {
		super(manager);
		lastGroupId = Integer.parseInt(
				manager.getHibernateHelper().getLastGroupId());
		groupModel = getGroups();
		}
	
	public Groups getGroups() {
		if ( groupModel == null){
			groupModel = manager.getHibernateHelper().listGroups();
		}
		return groupModel;
	}
		
	public Groups getUiGroups() {
		HashSet<GroupObject> set = new HashSet<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name(manager.getProperty("nameCheckbox")));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute(manager.getProperty("title"));
//			Pattern p = Pattern.compile(".*\\((.*)\\)");
//			Matcher m = p.matcher(title);
//			if (m.matches()){
//				title = m.group(1);
//			}
//			int startIndex = title.indexOf("(");
//			int endIndex = title.lastIndexOf(")");
//			title = title.substring(startIndex, endIndex);
			title = title.substring("Select (".length(), title.length()-")".length());
			GroupObject group = new GroupObject()
				.setName(title)
				.setId(checkbox.getAttribute(manager.getProperty("idCheckbox")));
			set.add(group);
		}
		return new Groups (set);
	}


	public void createGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		returnToGroupListPage();
		lastGroupId = lastGroupId + 1;
		groupObject.setId("" + lastGroupId);
		groupModel = groupModel.withAdded(groupObject);
	}

	public void modifyGroup(GroupObject groupObjectBefore, GroupObject groupObjectAfter) {
		manager.getNavigationHelper().goToGroupListPage();
		markGroup(groupObjectBefore);
		initGroupModification();
		fillGroupForm(groupObjectAfter);
		submitGroupModification();
		returnToGroupListPage();
		groupModel = groupModel.without(groupObjectBefore).withAdded(groupObjectAfter);
	}

	public void deleteGroup(GroupObject groupObject) {
			manager.getNavigationHelper().goToGroupListPage();
			markGroup(groupObject);
			click(By.name(manager.getProperty("deleteGroupLink")));
			returnToGroupListPage();
			groupModel = groupModel.without(groupObject);
	}

	private void markGroup(GroupObject group) {
	      findElement(By.xpath("//input[@value='" + group.id + "']")).click();
	}

	private void initGroupCreation() {
		click(By.name(manager.getProperty("initGroupCreationLink")));
	}

	private void initGroupModification() {
		click(By.name(manager.getProperty("initGroupModificationLink")));
	}

	private void fillGroupForm(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);
	}

	private void submitGroupCreation() {
		findElement(By.name(manager.getProperty("submitLink"))).click();
	}

	private void submitGroupModification() {
		click(By.name(manager.getProperty("submitGroupModificationLink")));
	}

	private void returnToGroupListPage() {
		click(By.linkText(manager.getProperty("returnToGroupListPageLink")));
	}

}
